package com.example.spritesheetanimation.view;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.example.spritesheetanimation.GameLoopThread;
import com.example.spritesheetanimation.R;
import com.example.spritesheetanimation.TempSprite;
import com.example.spritesheetanimation.actor.Animation;
import com.example.spritesheetanimation.actor.Fog;
import com.example.spritesheetanimation.actor.Sprite;
import com.example.spritesheetanimation.actor.Texture;

@SuppressLint("WrongCall")
public class GameView extends SurfaceView {

	private GameLoopThread gameLoopThread;

	private List<Sprite> sprites = new ArrayList<Sprite>();

	private List<TempSprite> temps = new ArrayList<TempSprite>();

	private long lastClick;

	private Bitmap bmpBlood;

	private Fog fog;

	private Texture tFog;
	private Animation aFog;

	public GameView(Context context) {

		super(context);

		if (gameLoopThread == null) {
			gameLoopThread = new GameLoopThread(this);
		}

		getHolder().addCallback(new SurfaceHolder.Callback() {

			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {

				boolean retry = true;

				gameLoopThread.setRunning(false);

				while (retry) {

					try {

						gameLoopThread.join();

						retry = false;

					} catch (InterruptedException e) {
					}

				}

			}

			@Override
			public void surfaceCreated(SurfaceHolder holder) {

				createSprites();

				gameLoopThread.setRunning(true);

				if (gameLoopThread.getState() == Thread.State.NEW) {
					gameLoopThread.start();
				}

				// gameLoopThread.start();

			}

			@Override
			public void surfaceChanged(SurfaceHolder holder, int format,

			int width, int height) {

			}

		});

		bmpBlood = BitmapFactory.decodeResource(getResources(),
				R.drawable.blood1);
		fog = new Fog(this, BitmapFactory.decodeResource(getResources(),
				R.drawable.fog));

		tFog = new Texture(getContext(), R.drawable.fog);

		aFog = new Animation(tFog, 1, 2, 300);

	}

	private void createSprites() {

		sprites.add(createSprite(R.drawable.bad1));

		sprites.add(createSprite(R.drawable.bad1));

		sprites.add(createSprite(R.drawable.bad1));

		sprites.add(createSprite(R.drawable.bad1));

		sprites.add(createSprite(R.drawable.bad1));

		sprites.add(createSprite(R.drawable.bad1));

	}

	private Sprite createSprite(int resouce) {

		Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);

		return new Sprite(this, bmp);

	}

	@Override
	public void onDraw(Canvas canvas) {

		if (canvas != null) {
			canvas.drawColor(Color.BLACK);

			for (int i = temps.size() - 1; i >= 0; i--) {

				temps.get(i).onDraw(canvas);

			}

			for (Sprite sprite : sprites) {

				sprite.onDraw(canvas);

			}

			// fog.onDraw(canvas);

			// tFog.onDraw(canvas, 0, 0);
			aFog.update();
			aFog.onDraw(canvas, 0, 0);
		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		if (System.currentTimeMillis() - lastClick > 300) {

			lastClick = System.currentTimeMillis();

			float x = event.getX();

			float y = event.getY();

			synchronized (getHolder()) {

				for (int i = sprites.size() - 1; i >= 0; i--) {

					Sprite sprite = sprites.get(i);

					if (sprite.isCollition(x, y)) {

						sprites.remove(sprite);

						temps.add(new TempSprite(temps, this, x, y, bmpBlood));

						break;

					}

				}

			}

		}

		return true;

	}

}
